package com.example.dragbtwtworecyclerviews

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * @author Burwei
 */
class MainFragment : Fragment(), BaseRecyclerviewAdaptor.OnClickListener {

    private lateinit var myRecyclerviewLeft: RecyclerView
    private lateinit var myRecyclerviewAdaptorLeft: BaseRecyclerviewAdaptor
    private lateinit var myViewManagerLeft: RecyclerView.LayoutManager
    private lateinit var myRecyclerviewRight: RecyclerView
    private lateinit var myRecyclerviewAdaptorRight: BaseRecyclerviewAdaptor
    private lateinit var myViewManagerRight: RecyclerView.LayoutManager
    private val dragListener = MyDragListener()
    private val testDataLeft = mutableListOf<Any>("猫", "狗", "老虎", "牛", "羊", "鸡")
    private val testDataRight = mutableListOf<Any>("苹果", "橘子", "香蕉", "葡萄", "李子", "哈密瓜")


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate布局
        myViewManagerLeft = LinearLayoutManager(activity)
        myRecyclerviewAdaptorLeft = BaseRecyclerviewAdaptor()
        myRecyclerviewAdaptorLeft.setClickListener(this)
        myRecyclerviewAdaptorLeft.setDragListener(dragListener)
        myViewManagerRight = LinearLayoutManager(activity)
        myRecyclerviewAdaptorRight = BaseRecyclerviewAdaptor()
        myRecyclerviewAdaptorRight.setClickListener(this)
        myRecyclerviewAdaptorRight.setDragListener(dragListener)
        var rootView = inflater.inflate(R.layout.fragment_main, container, false) as View
        myRecyclerviewLeft = rootView.findViewById<RecyclerView>(R.id.recyclerviewLeft)
                .apply {
                    // 如果您知道发生变化，请使用此设置来提高性能
                    // 在内容中不改变RecyclerView的布局大小
                    setHasFixedSize(true)

                    // 使用linear layout manager
                    layoutManager = myViewManagerLeft

                    // 指定一个 viewAdapter（另请参见下一个示例）
                    adapter = myRecyclerviewAdaptorLeft
                }
        myRecyclerviewRight = rootView.findViewById<RecyclerView>(R.id.recyclerviewRight)
                .apply {
                    // 如果您知道会发生变化，请使用此设置来提高性能
                    // 在内容中不改变RecyclerView的布局大小
                    setHasFixedSize(true)

                    // 使用 linear layout manager
                    layoutManager = myViewManagerRight

                    // 指定一个 viewAdapter（另请参见下一个示例）
                    adapter = myRecyclerviewAdaptorRight
                }
        return rootView
    }

    override fun onStart() {
        super.onStart()
        myRecyclerviewAdaptorLeft.setData(testDataLeft)
        myRecyclerviewAdaptorRight.setData(testDataRight)
    }

    override fun recyclerviewClick(name: String) {
        println("获取名称: $name")
    }
}